package com.lkbaby.flowable;

import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/test")
public class MyProcess {

	@Autowired
	private RepositoryService repositoryService;

	@Autowired
	private RuntimeService runtimeService;

	@Autowired
	private TaskService taskService;

	/**
	 * 流程部署
	 *
	 * @throws Exception
	 */
	@RequestMapping("/deploy")
	public void testDeploy() {

		List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery()
				.processDefinitionKey("flowableAndSign").list();
		for (ProcessDefinition definition : processDefinitions) {
			System.out.println(definition.getDeploymentId());
			repositoryService.deleteDeployment(definition.getDeploymentId(), true);
		}

		Deployment deploy = repositoryService.createDeployment()
				.addClasspathResource("processes/并加签.bpmn20.xml")
				.name("并加签")
				.deploy();
		System.out.println("deploy.getId() = " + deploy.getId());
		System.out.println("deploy.getName() = " + deploy.getName());
		System.out.println("部署开始的时间：" + new Date());
		//TimeUnit.MINUTES.sleep(3);
	}

	/**
	 * 启动流程
	 *
	 * @throws Exception
	 */
	@RequestMapping("/startFlow")
	public void startFlow() {
		// 获取process
		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
				.processDefinitionKey("flowableAndSign").singleResult();
		Map<String, Object> map = new HashMap<>();
		// 设置多人会签的数据
		map.put("persons", Arrays.asList("张三", "李四"));
		map.put("status", false);
		//拒绝人数
		map.put("reject", 0);
		//同意人数
		map.put("agree", 0);
		ProcessInstance processInstance = runtimeService
				.startProcessInstanceById(processDefinition.getId(), map);
	}

	/**
	 * 完成一个任务
	 */
	@RequestMapping("/completeOne")
	public void completeTask1(@RequestParam String userKey) {

		Task task = taskService.createTaskQuery()
				.processDefinitionKey("flowableAndSign")
				.taskAssignee(userKey)
				.singleResult();

		if (task != null) {
			Map<String, Object> map = new HashMap<>();
			// 同意 agree + 1
			Integer agree = (int) taskService.getVariable(task.getId(), "agree");
			map.put("agree", agree + 1);
			taskService.complete(task.getId(), map);
		}

	}

	/**
	 * 并加签
	 */
	@RequestMapping("/addSign")
	public void addSign(@RequestParam String userKey) {

		Task task = taskService.createTaskQuery()
				.processDefinitionKey("flowableAndSign")
				.taskAssignee(userKey)
				.singleResult();
		System.out.println("Task ID：" + task.getTaskDefinitionKey()
				+ "\nTask Name：" + task.getName()
				+ "\n流程实例ID：" + task.getProcessInstanceId());
		// runtimeService.addMultiInstanceExecution(String activityId, String parentExecutionId, Map<String, Object> executionVariables)
		// runtimeService.addMultiInstanceExecution(task.getTaskDefinitionKey(), task.getProcessInstanceId(), Collections.singletonMap("person", (Object) "王五"));
		runtimeService.addMultiInstanceExecution(task.getTaskDefinitionKey(), task.getProcessInstanceId(),
				new HashMap<String, Object>() {{
					put("person", "王五");
				}});
	}

}
